package com.example.gulimall.ware.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.example.common.utils.R;
import com.example.gulimall.ware.feign.MemberFeignService;
import com.example.gulimall.ware.vo.FareVo;
import com.example.gulimall.ware.vo.MemberAddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.utils.PageUtils;
import com.example.common.utils.Query;

import com.example.gulimall.ware.dao.WareInfoDao;
import com.example.gulimall.ware.entity.WareInfoEntity;
import com.example.gulimall.ware.service.WareInfoService;



@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {
    @Autowired
    MemberFeignService memberFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                new QueryWrapper<WareInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageCondition(Map<String, Object> params) {
        QueryWrapper<WareInfoEntity> wrapper=new QueryWrapper<WareInfoEntity>();
        if (params.containsKey("key")){
            String key=params.get("key").toString();
            wrapper.eq("id",key).or().like("name",key)
                    .or().like("address",key)
                    .or().like("areacode",key);
        }
        //this.page 人人开源封装的分页条件查询方法，底层调的是WareInfoDao.selectPage()
        //也就是mybatis plus 提供的分页方法
        IPage<WareInfoEntity> page=this.page(
                new Query<WareInfoEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }


    @Override
    public FareVo getFare(Long addrId) {
        FareVo fareVo = new FareVo();
        R r = memberFeignService.addrInfo(addrId);
        if (r!=null){
            MemberAddressVo data = r.getData("memberReceiveAddress",new TypeReference<MemberAddressVo>() {
            });
            if(data!=null){
                //模拟运费，截取手机号，如果要具体可以去查询快递公司的第三方接口
                String phone = data.getPhone();
                String substring = phone.substring(phone.length() - 1, phone.length());
                BigDecimal bigDecimal = new BigDecimal(substring);
                fareVo.setAddressVo(data);
                fareVo.setFare(bigDecimal);
                return  fareVo;
            }
        }
        return null;

    }

}